﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;
using MockupDesigner.AppItems;

namespace MockupDesigner.Items
{
    public abstract class ContentControlContainerItem : ResizableItem
    {
        public static readonly DependencyProperty SelectedIndexProperty =
           DependencyProperty.Register("SelectedIndex", typeof(int), typeof(TabControlItem));

        [Category("Prop_Content")]
        public int SelectedIndex
        {
            get
            {
                return (int)GetValue(SelectedIndexProperty);
            }
            set
            {
                if (SelectedIndex == value)
                    return;
                if (value >= ItemsSource.Count())
                {
                    DesignerItem parent = this.Parent as DesignerItem;
                    if (parent != null)
                    {
                        parent.ReloadPropertyGrid();
                        return;
                    }
                }
                int oldIndex = SelectedIndex;
                if (!autochanging)
                {
                    OnPropertyChanging("SelectedIndex", SelectedIndex, value);
                    SetValue(SelectedIndexProperty, value);
                    SetNewSelectedIndex(oldIndex);
                    OnPropertyChanged("SelectedIndex");
                }
                else
                {
                    SetValue(SelectedIndexProperty, value);
                    SetNewSelectedIndex(oldIndex);
                }
            }
        }

        protected abstract void SetNewSelectedIndex(int oldIndex);

        public static readonly DependencyProperty ItemsSourceProperty =
           DependencyProperty.Register("ItemsSource", typeof(ContentControl[]), typeof(ListBoxItem));

        public ContentControl[] ItemsSource
        {
            get
            {
                return (ContentControl[])GetValue(ItemsSourceProperty);
            }
            set
            {
                autochanging = true;
                if (AreItemsEqual(value))
                    return;
                OnPropertyChanging("ItemsSource", ItemsSource, value);
                SetValue(ItemsSourceProperty, value);
                SelectedIndex = 0;
                OnPropertyChanged("ItemsSource");
                autochanging = false;
            }
        }

        [Category("Prop_Content")]
        public int Count
        {
            get
            {
                return ItemsSource == null ? 0 : ItemsSource.Count();
            }
        }

        private bool AreItemsEqual(ContentControl[] value)
        {
            if (ItemsSource == null && value == null)
                return true;
            if (ItemsSource == null && value != null)
                return false;
            if (ItemsSource != null && value == null)
                return false;
            if (ItemsSource.Count() != value.Count())
            {
                return false;
            }
            if (ItemsSource.Count() > 0)
            {
                for (int i = 0; i < ItemsSource.Count(); i++)
                {
                    if (ItemsSource[i].Content.ToString() != value[i].Content.ToString())
                        return false;
                }
            }
            return true;
        }
    }
}
